//
//  CYSelectCarVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/17.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYSelectCarVC.h"
#import "CYAddCarTableVC.h"
#import "CYJumpsTool.h"
#import "CYDiscoveryHttpTool.h"
#import "CYLoginHttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYCarInfoCell.h"
#import "UITableViewCell+Category.h"
#import "CYCarInfo.h"
#import "UIImageView+WebCache.h"
#import "CYCompanyUserTool.h"
#import "CYTool.h"

@interface CYSelectCarVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, weak) UITableView *tableview;
@property(nonatomic, strong) NSArray *cars;
@property(nonatomic, copy) CYSelectedCarBlock block;
@end

@implementation CYSelectCarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择车辆";
    [self setupTable];
    [CYTool setExtraCellLineHiddenWithTableView:self.tableview];
    [self setupItem];
    [self loadData];
}

- (void)setupItem {
    UIBarButtonItem *addItme = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
    self.navigationItem.rightBarButtonItem = addItme;
}

- (void)add {
    CYAddCarTableVC *addCar = [CYAddCarTableVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    addCar.userType = self.userType;
    CYWeakObject(weakself, self);
    [addCar completion:^{
        [weakself loadData];
    }];
    [self.navigationController pushViewController:addCar animated:YES];
}

- (void)setupTable {
    UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:table];
    table.dataSource = self;
    table.delegate = self;
    self.tableview = table;
}

- (void)loadData {
    NSMutableDictionary *param = nil;
    if (self.userType == CYUserTypePerson) {
        CYUser *user = [CYLoginHttpTool userData].user;
        if (user) {
            param = [NSMutableDictionary dictionaryWithDictionary:@{@"userId": user.userId, @"token": user.token, @"page": @"0"}];
        }
        
    } else if (self.userType == CYUserTypeCompany) {
        CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
        if (user) {
            param = [NSMutableDictionary dictionaryWithDictionary:@{@"userId": user.carCompanyUserId, @"token": user.token, @"page": @"0"}];
        }
    }
    if (param) {
        [MBProgressHUD showMessage:@"加载中" toView:self.view];
        [CYDiscoveryHttpTool carInfoListWithParam:param success:^(NSArray *cars) {
            [MBProgressHUD hideHUDForView:self.view];
            self.cars = cars;
            [self.tableview reloadData];
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (message) {
                [MBProgressHUD showError:message];
            }
        }];
    } else {
        //调整到登录页面
        
    }
}

#pragma mark - tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CYCarInfo *car = self.cars[indexPath.row];
    
    CYCarInfoCell *cell = [CYCarInfoCell loadFromNibUsingClassName];
    cell.carName.text = car.vehicleType;
    if (car.photo && !car.photo.isEmpty) {
        [cell.carImgeView sd_setImageWithURL:CYURLWithUserService(car.photo) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
    }
    cell.carFrameNumber.text = CYStringFormat(@"车架号:%@", car.carFrameNumber);
    cell.carEngineNumber.text = CYStringFormat(@"发动机号:%@", car.carEngineNumber);
    cell.plateNumber.text = CYStringFormat(@"车牌号:%@", car.plateNumber);
    if (car.type.intValue == 0) {
        if (car.examineStatus.intValue == 2) {
            cell.statusView.backgroundColor = CYNaviThemeColor;
        } else if (car.examineStatus.intValue == -1) {
            cell.statusView.backgroundColor = [UIColor redColor];
        } else {
            cell.statusView.backgroundColor = [UIColor orangeColor];
        }
    } else {
        cell.statusView.backgroundColor = CYNaviThemeColor;
    }
    if (car.carStatus.intValue == 2) {
        cell.mark.hidden = NO;
    } else {
        cell.mark.hidden = YES;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CYCarInfo *car = self.cars[indexPath.row];
    if (car.type.intValue == 0) {//个人
        if (car.examineStatus.intValue == 2) {
            if (car.carStatus.intValue == 2) {
                [MBProgressHUD showError:@"车辆租赁中，请选择其他车辆"];
            } else {
                //选择
                if (self.block) {
                    self.block(car);
                }
                self.block = nil;
                [self.navigationController popViewControllerAnimated:YES];
            }
        } else {
            [MBProgressHUD showError:@"该条信息还未通过审核"];
        }
    } else if (car.type.intValue == 1) {//企业
        if (car.carStatus.intValue == 2) {
            [MBProgressHUD showError:@"车辆租赁中，请选择其他车辆"];
        } else {
            //选择
            if (self.block) {
                self.block(car);
            }
            self.block = nil;
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)selectedCarInfo:(CYSelectedCarBlock)selectedData {
    self.block = selectedData;
}

@end
